﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class UserForm : ChurchForm
    {
        #region Fields / Properties
        private EmployeeBO _eBO = new EmployeeBO();
        private ScreenMode _currentMode = ScreenMode.View;
        private Employee _emp;
        private ErrorProvider _errorProvider;

        enum ScreenMode
        {
            View, Add, Edit
        }
        #endregion Fields / Properties


        #region Constructor and Load
        public UserForm()
        {
            InitializeComponent();
            this.Title = "UserForm";
            this._errorProvider = new ErrorProvider();
            this._errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            this.cboClearanceLevel.Items.Add(0);
            this.cboClearanceLevel.Items.Add(1);
            this.cboClearanceLevel.Items.Add(2);
            this.cboClearanceLevel.Items.Add(3);
            this.cboClearanceLevel.Items.Add(4);
            this.RefreshEmployeeGrid();
        }

        #endregion Constructor and Load


        #region Populate Methods
        private void RefreshEmployeeGrid()
        {
            List<EmployeeAdminView> lstEmp = _eBO.GetActiveRecords();
            this.grdUsers.DataSource = lstEmp;
            this.grdUsers.Columns["EmployeePK"].HeaderText = "Employee #";
            this.grdUsers.Columns["FirstName"].HeaderText = "First Name";
            this.grdUsers.Columns["LastName"].HeaderText = "Last Name";
        }

        private void LoadEmployeeDetails()
        {
            int employeePK = (int)this.grdUsers.SelectedRows[0].Cells["EmployeePK"].Value;
            _emp = this._eBO.GetRecordByPK(employeePK);

            this.txtFirstName.Text = _emp.FirstName;
            this.txtLastName.Text = _emp.LastName;
            this.txtLoginID.Text = _emp.LoginID;
            this.cbxAdmin.Checked = _emp.AdminInd;
            this.cbxViewOnly.Checked = _emp.ViewOnlyInd;
            this.cboClearanceLevel.SelectedIndex = this.cboClearanceLevel.Items.IndexOf(_emp.ClearanceLevel);
            this.txtFirstName.Focus();
        }

        private void ClearDetails()
        {
            this.txtFirstName.Text = "";
            this.txtLastName.Text = "";
            this.txtLoginID.Text = "";
            this.txtPassword.Text = "";
            this.cbxViewOnly.Checked = false;
            this.cbxAdmin.Checked = false;
            this.cboClearanceLevel.SelectedIndex = -1;
        }

        #endregion Populate Methods


        #region Mode Transition Methods
        private void EnterEditMode()
        {
            this.grpDetails.Enabled = true;
            this.grdUsers.Enabled = false;
            this.btnAdd.Enabled = false;
            this.btnEdit.Enabled = false;
            this.btnDelete.Enabled = false;
        }

        private void LeaveEditMode()
        {
            this.grpDetails.Enabled = false;
            this.grdUsers.Enabled = true;
            this.btnAdd.Enabled = true;
            this.btnEdit.Enabled = true;
            this.btnDelete.Enabled = true;
            this._errorProvider.Clear();
            this._errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
        }
        #endregion Mode Transition Methods


        #region Update Methods
        private void SaveIt()
        {
            if (this.ValidateEntries())
            {
                if (this._currentMode == ScreenMode.Add)
                {
                    this._emp = new Employee();
                    this._emp.ActiveInd = true;
                }

                this._emp.FirstName = this.txtFirstName.Text.Trim();
                this._emp.LastName = this.txtLastName.Text.Trim();
                this._emp.LoginID = this.txtLoginID.Text.Trim();
                if (this.txtPassword.Text.Trim().Length > 0)
                    this._emp.Password = this.txtPassword.Text.Trim();
                this._emp.ViewOnlyInd = this.cbxViewOnly.Checked;
                this._emp.AdminInd = this.cbxAdmin.Checked;
                this._emp.ClearanceLevel = (int)this.cboClearanceLevel.SelectedItem;

                this._eBO.UpdateEmployee(this._emp, this._currentMode == ScreenMode.Add);

                this._currentMode = ScreenMode.View;
                this.ClearDetails();
                this.LeaveEditMode();
                this.RefreshEmployeeGrid();
            }
        }

        private void DeleteEmployee()
        {
            int employeePK = (int)this.grdUsers.SelectedRows[0].Cells["EmployeePK"].Value;
            this._eBO.SetToInactive(employeePK);
            this.RefreshEmployeeGrid();
        }

        private bool ValidateEntries()
        {
            this._errorProvider.Clear();
            this._errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;

            bool valid = true;

            if (this.txtFirstName.Text.Trim().Length == 0)
            {
                this._errorProvider.SetError(this.txtFirstName, "Required");
                valid = false;
            }
            if (this.txtLastName.Text.Trim().Length == 0)
            {
                this._errorProvider.SetError(this.txtLastName, "Required");
                valid = false;
            }
            if (this.txtLoginID.Text.Trim().Length == 0)
            {
                this._errorProvider.SetError(this.txtLoginID, "Required");
                valid = false;
            }
            if (this._currentMode == ScreenMode.Add && this.txtPassword.Text.Trim().Length == 0)
            {
                this._errorProvider.SetError(this.txtPassword, "Required");
                valid = false;
            }
            if (this.cboClearanceLevel.SelectedIndex < 0)
            {
                this._errorProvider.SetError(this.cboClearanceLevel, "Required");
                valid = false;
            }

            return valid;
        }

        #endregion Update Methods


        #region Control Events

        private void grdUsers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.btnEdit.PerformClick();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this._currentMode = ScreenMode.Add;
            this.EnterEditMode();
            this.txtFirstName.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.grdUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("You must select the row that you wish to edit");
                return;
            }

            this._currentMode = ScreenMode.Edit;
            this.EnterEditMode();
            this.LoadEmployeeDetails();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.grdUsers.SelectedRows.Count == 0)
                MessageBox.Show("Please select the user that you wish to delete");

            if (DialogResult.OK == MessageBox.Show("Are you sure you want to delete them?", "Confirm Delete", MessageBoxButtons.OKCancel))
                this.DeleteEmployee();
        }

        private void btnSaveDetails_Click(object sender, EventArgs e)
        {
            this.SaveIt();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this._currentMode = ScreenMode.View;
            this.ClearDetails();
            this.LeaveEditMode();
        }

        #endregion Control Events
    }
}
